package com.circus.trace.controller.material.area;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.circus.trace.bean.material.area.Area;
import com.circus.trace.biz.material.area.AreaService;
import com.circus.trace.controller.BaseController;
import com.circus.trace.ext.shiro.anno.SINORequiresPermissions;
import com.circus.trace.vo.InstallPermission;

import club.zhcs.common.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("area")
public class AreaController extends BaseController {
	@Autowired
	AreaService areaService;

	/**
	 * 用户列表
	 * 
	 * @param page
	 *            页码
	 * @return
	 */
	@GetMapping("list")
	@SINORequiresPermissions(InstallPermission.AREA_LIST)
	@ApiOperation(value = "产区分页列表")
	public Result list(@RequestParam(value = "page", defaultValue = "1") @ApiParam("页码") int page) {
		return Result.success().addData("pager", areaService.searchByPage(fixPage(page), Cnd.where("enabled", "=", true).desc("id")));
	}

	/**
	 * 
	 * @param key
	 *            检索条件
	 * @param page
	 *            页码
	 * @return
	 */
	@GetMapping("search")
	@SINORequiresPermissions(InstallPermission.AREA_LIST)
	@ApiOperation(value = "产区分页检索")
	public Result search(@RequestParam("key") @ApiParam("搜索关键字") String key, @RequestParam(value = "page", defaultValue = "1") @ApiParam("页码") int page) {
		return Result.success()
				.addData("pager", areaService.searchByKeyAndPage(
						fixSearchKey(key),
						fixPage(page),
						Cnd.where("enabled", "=", true),
						"name")
						.addParam("key", key));
	}

	@GetMapping("all")
	public Result all() {
		return Result.success().addData("areas", areaService.query(Cnd.where("enabled", "=", true)));
	}

	@PostMapping(value = "add")
	@SINORequiresPermissions(InstallPermission.AREA_ADD)
	@ApiOperation(value = "新增产区")
	public Result save(@RequestBody Area area) {
		return areaService.save(area) == null ? Result.fail("保存产区信息失败!") : Result.success().addData("area", area);
	}

	@PostMapping(value = "edit")
	@SINORequiresPermissions(InstallPermission.AREA_EDIT)
	public Result edit(@RequestBody Area area) {
		return areaService.update(area) == 1 ? Result.success() : Result.fail("更新产区失败!");
	}
	
	/**
	 * 删除产区
	 * 
	 * @param id
	 *            产区id
	 * @return
	 */
	@GetMapping("delete/{id}")
	@SINORequiresPermissions(value = { InstallPermission.AREA_DELETE })
	@ApiOperation("删除产区")
	public Result delete(@PathVariable("id") @ApiParam("产区id") long id) {
		return areaService.update(Chain.make("enabled", false), Cnd.where("id", "=", id)) == 1 ? Result.success() : Result.fail("删除产区失败!");
	}
}
